package com.jing.android.notes.ui.notifications;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.RemoteInput;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.jing.android.notes.R;
import com.jing.android.notes.constant.Constants;
import com.jing.android.notes.constant.Keys;
import com.jing.android.notes.receiver.NotificationReceiver;

public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        final TextView textView = root.findViewById(R.id.text_notifications);
        notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        root.findViewById(R.id.btn_show_notification).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(v.getContext());
                if (notificationManager != null) {
                    Intent intent = new Intent(v.getContext(), NotificationDetailActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    PendingIntent pIntent = PendingIntent.getActivity(v.getContext(), 0, intent,
                            PendingIntent.FLAG_ONE_SHOT);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(v.getContext(), Constants.NOTIFICATION_CHANNEL_ID_NOTES)
                            .setContentTitle("ContextTitle: " + "测试Title")
//                            .setContentText("ContextText: 测试Content")
                            .setContentIntent(pIntent)
                            .setAutoCancel(true)
                            .setSmallIcon(R.mipmap.ic_launcher)
//                            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText("BigText: 测试Content -> Big"))
                            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        //安卓10(API 29),系统自动生成按钮,测试Style-测试通知-测试通知-测试通知-测试通知-测试通知-测试通知,
                    }
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        //安卓8.0(API 26),必须设置channel
                        NotificationChannel channel = notificationManager.getNotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_NOTES);
//                        if (channel == null) {
                        channel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID_NOTES, getString(R.string.notes),
                                NotificationManager.IMPORTANCE_DEFAULT);
                        notificationManager.createNotificationChannel(channel);
//                        }
                    }
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        //安卓7.0(API 24),可以添加一个操作在通知栏直接回复

                    }
                    RemoteInput remoteInput = new RemoteInput.Builder(Keys.NOTIFICATION_REPLY)
                            .setLabel("立即回复")
                            .build();
                    Intent replyIntent = new Intent(NotificationReceiver.ACTION_REPLY);
                    replyIntent.putExtra(Keys.NOTIFICATION_ID, 1);
                    PendingIntent replyPendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 1,
                            replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Action action = new NotificationCompat.Action.Builder(0, "回复", replyPendingIntent)
                            .addRemoteInput(remoteInput)
                            .build();
                    builder.addAction(action);
                    notificationManager.notify("test", 1, builder.build());
                }
            }
        });
        return root;
    }
}